package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class CustormerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 条件查询客户列表
     *
     * @param
     * @return
     */
    @PostMapping("/customer/list")
    @ResponseBody
    public Map<String, Object> supplierList(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "30") Integer rows,
                                            String customerName) {

        Map<String, Object> map = customerService.getCustomerList(page, rows, customerName);
        return map;
    }

    /**
     * 新增或修改客户信息
     */
    @PostMapping("/customer/save")
    @ResponseBody
    public ServiceVO saveOrUpdate(Customer customer) {

        if (customer == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        customerService.saveOrUpdate(customer);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除客户信息
     */
    @PostMapping("/customer/delete")
    @ResponseBody
    public ServiceVO customerDelete(@RequestParam String ids) {
        // "1,2,3"
//        String ids = request.getParameter("ids");

        if (StringUtils.isEmpty(ids)) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        customerService.deleteByIds(ids);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


}
